import Config from "@/config";
const baseURL = Config.baseUrl;
const statusName = Config.response.statusName;
const statusCode = Config.response.statusCode;
const msgName = Config.response.msgName;
const dataName = Config.response.dataName;
const logoutUrl = Config.logoutUrl;
const tokenName = Config.request.tokenName;
/* console.log({
  baseURL,
  statusName,
  statusCode,
  msgName,
  dataName,
  logoutUrl,
  tokenName
}); */

class Tool {
  constructor() {
    this.baseUrl = baseURL;
    this.code = statusName;
    this.status = statusCode;
    this.message = msgName;
    this.data = dataName;
    this.logout_url = logoutUrl;
    this.token = tokenName;
  }
  setTitle(title) {
    window.document.title = title || "admin";
  }
  /*时间转换*/
  checkTime(i) {
    if (i < 10) {
      i = "0" + i;
    }
    return i;
  }
  formatTime(dateNum, type) {
    var date = new Date(dateNum * 1000);
    var getFullYear = this.checkTime(date.getFullYear());
    var getMonth = this.checkTime(date.getMonth() + 1);
    var getDate = this.checkTime(date.getDate());
    var getHours = this.checkTime(date.getHours());
    var getMinutes = this.checkTime(date.getMinutes());
    var getSeconds = this.checkTime(date.getSeconds());
    if (type === "day") {
      return getFullYear + "-" + getMonth + "-" + getDate;
    }
    return (
      getFullYear +
      "-" +
      getMonth +
      "-" +
      getDate +
      " " +
      getHours +
      ":" +
      getMinutes +
      ":" +
      getSeconds
    );
  }
  /*类型验证*/
  checkStr(str, type) {
    switch (type) {
      case "phone":
        //手机号码
        return /^1[3|4|5|7|8][0-9]{9}$/.test(str);
      case "tel":
        //座机
        return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
      case "card":
        //身份证
        return /^\d{15}|\d{18}$/.test(str);
      case "pwd":
        //密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线
        return /^[a-zA-Z]\w{5,17}$/.test(str);
      case "postal":
        //邮政编码
        return /[1-9]\d{5}(?!\d)/.test(str);
      case "QQ":
        //QQ号
        return /^[1-9][0-9]{4,9}$/.test(str);
      case "email":
        //邮箱
        return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
      case "money":
        //金额(小数点2位)
        return /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(
          str
        );
      case "URL":
        //网址
        return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(
          str
        );
      case "IP":
        //IP
        return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(
          str
        );
      case "date":
        //日期时间
        return (
          /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(
            str
          ) || /^(\d{4})\-(\d{2})\-(\d{2})$/.test(str)
        );
      case "number":
        //数字
        return /^[0-9]$/.test(str);
      case "integers":
        //数字
        return /^-?[1-9]\d*$/.test(str);
      case "integer":
        //非负整数
        return /^(0|[1-9]\d*)$/.test(str);
      case "integer5":
        //非负整数1-5
        return /^[0-5]$/.test(str);
      case "english":
        //英文
        return /^[a-zA-Z]+$/.test(str);
      case "chinese":
        //中文
        return /^[\u4E00-\u9FA5]+$/.test(str);
      case "lower":
        //小写
        return /^[a-z]+$/.test(str);
      case "upper":
        //大写
        return /^[A-Z]+$/.test(str);
      case "HTML":
        //HTML标记
        return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
      default:
        return true;
    }
  }
  /* formData的中有空值，则返回false */
  formDataCheck(object) {
    for (let key in object) {
      if (!object[key]) {
        return false;
      }
    }
    return true;
  }
}

export default Tool;
